package org.miyuki4j.cfgtool;

import org.miyuki4j.cfgtool.app.CommandCfgApp;
import org.miyuki4j.cfgtool.app.GuiCfgApp;
import org.miyuki4j.cfgtool.app.WebCfgApp;

import java.util.ArrayList;
import java.util.List;

/**
 * @Desc
 * @Author miyuki4j
 * @Date 2025/7/28/星期一 17:42
 * @Version 1.0
 */
public class CfgAppFactory {

    public static CfgApp auto(String[] args) {
        List<RunModeEnum> modes = new ArrayList<>();
        for (String arg : args) {
            if (RunModeEnum.COMMAND.match(arg)) {
                modes.add(RunModeEnum.COMMAND);
            } else if (RunModeEnum.GUI.match(arg)) {
                modes.add(RunModeEnum.GUI);
            } else if (RunModeEnum.WEB.match(arg)) {
                modes.add(RunModeEnum.WEB);
            }
        }
        if (modes.isEmpty()) {
            return createApp(RunModeEnum.COMMAND);
        } else if (modes.size() > 1) {
            throw new IllegalArgumentException("只能使用一个运行模式, args:" + String.join(" ", args));
        }
        return createApp(modes.get(0));
    }

    private static CfgApp createApp(RunModeEnum mode) {
        switch (mode) {
            case COMMAND:
                return new CommandCfgApp();
            case GUI:
                return new GuiCfgApp();
            case WEB:
                return new WebCfgApp();
            default:
                throw new RuntimeException("Invalid run mode: " + mode);
        }
    }
}
